package com.ns.school.controller.edu;

import com.github.pagehelper.PageHelper;
import com.ns.school.common.utils.CommonUtil;
import com.ns.school.po.edu.BookDB;
import com.ns.school.po.edu.LayuiResult;
import com.ns.school.service.edu.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 图书管理
 */
@Controller
public class BookController {
    @Autowired
    private BookService bookService;


    @RequestMapping("/book/list")
    public String list(Model model) {
        return "view/book/list";
    }

    @RequestMapping("/book/add")
    public String add(Model model) {
        return "view/book/add";
    }

    @RequestMapping("/book/edit")
    public String edit(Model model, Integer id, ModelMap modelMap) {
        BookDB bookDB = (BookDB) bookService.editBook(id);// 此处需要通过参数id获取到具体的图书对象
        modelMap.put("book", bookDB);
        return "view/book/edit";
    }

    //图书的批量删除
    @RequestMapping("bookUpdate")
    @ResponseBody
    public LayuiResult<BookDB> bookUpdate(BookDB bookDB,String ids) {
        LayuiResult<BookDB> result = new LayuiResult<>();
        String[] strarray = ids.split(",");
        //删除
        for (int i = 0; i < strarray.length; i++) {
            if (!strarray[i].isEmpty()) {
                result.setCode(0);
                result.setMsg("批量删除成功");
                bookService.bookUpdate(Integer.valueOf(strarray[i]));
                return result;

            } else {
                result.setCode(500);
                result.setMsg("删除失败");
            }

        }
        return result;
    }


        //  添加书籍
        @RequestMapping("addBook") //   用来处理请求地址映射的注解
        @ResponseBody   // 通常用来返回JSON数据给客户端
        public LayuiResult<BookDB> addBook (@RequestBody BookDB bookDB){
            LayuiResult<BookDB> result = new LayuiResult<>();
            bookService.addBookMapper(result,bookDB);
            return result;
        }



    //  修改书籍信息
    @RequestMapping("/updateBook")
    @ResponseBody
    public LayuiResult<BookDB> upBook(@RequestBody BookDB bookDB) throws Exception {
        LayuiResult result = new LayuiResult();
       Integer a = bookService.updBook(bookDB);
        if(a>0) {
            //修改提示
            result.setCode(0);
            result.setMsg("修改成功！");


        }
        return result;
    }

    //获取书籍列表
    @RequestMapping("findallbooks")
    @ResponseBody
    public Object findallbooks(BookDB bookDB, Integer page, Integer limit) {
        PageHelper.startPage(page, limit);
        bookService.findAll(bookDB);
        List<BookDB> listAll = bookService.findAllBooks(bookDB);
        return CommonUtil.getStringObjectMap(listAll);
    }

    //删除书籍
    @RequestMapping("deleteBooks")
    @ResponseBody
    public LayuiResult<BookDB> deleteBooks(BookDB bookDB) {
        LayuiResult result = new LayuiResult();
        bookService.deleteBooks(result,bookDB);
        return result;
    }


    //图书的上架
    @RequestMapping("shelvesBooks")
    @ResponseBody
    public LayuiResult<BookDB> shelvesBooks(BookDB bookDB) {
        LayuiResult result = new LayuiResult();

        bookService.shelvesBooks(result,bookDB);

        return result;
    }

    //图书的下架
    @RequestMapping("frameBooks")
    @ResponseBody
    public LayuiResult<BookDB> frameBooks(BookDB bookDB) {
        LayuiResult result = new LayuiResult();
        bookService.frameBooks(result,bookDB);
        return result;

    }




}
